package com.example.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.example.annotation.PassToken;
import com.example.entity.Python;
import com.example.entity.python.GetResourceDto;
import com.example.entity.python.PythonExcel;
import com.example.service.IPythonService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author ChenXiaoBin
 * on 2019-10-15
 */
@RestController
@RequestMapping("/python/V1.0")
public class PythonController {

    @Autowired
    private IPythonService pythonService;

    @PutMapping(value = "")
    @ApiOperation(value = "测试", notes = "测试")
    public void save(@RequestBody GetResourceDto getResourceDto) {

        pythonService.getResource(getResourceDto);
    }

    @PutMapping(value = "/json/analysis")
    @ApiOperation(value = "解析", notes = "解析")
    @PassToken
    public void jsonAnalysis() {
        String jsonMessage =
                "{\"blocks\":[{\"key\":\"eilgi\",\"text\":\"1:第一条\",\"type\":\"unstyled\",\"depth\":0,\"inlineStyleRanges\":[],\"entityRanges\":[],\"data\":{}},{\"key\":\"81l0e\",\"text\":\"2:第二条www.baidu.com\",\"type\":\"unstyled\",\"depth\":0,\"inlineStyleRanges\":[],\"entityRanges\":[{\"offset\":5,\"length\":13,\"key\":0}],\"data\":{}},{\"key\":\"23crh\",\"text\":\"3:第三天:www.baiud 和网易www.wangye\",\"type\":\"unstyled\",\"depth\":0,\"inlineStyleRanges\":[],\"entityRanges\":[{\"offset\":6,\"length\":9,\"key\":1},{\"offset\":19,\"length\":10,\"key\":2}],\"data\":{}},{\"key\":\"8u3qs\",\"text\":\"4:  第四www\",\"type\":\"unstyled\",\"depth\":0,\"inlineStyleRanges\":[],\"entityRanges\":[{\"offset\":6,\"length\":3,\"key\":3}],\"data\":{}}],\"entityMap\":{\"0\":{\"type\":\"LINK\",\"mutability\":\"MUTABLE\",\"data\":{\"href\":\"www.baidu.com\",\"target\":\"\"}},\"1\":{\"type\":\"LINK\",\"mutability\":\"MUTABLE\",\"data\":{\"href\":\"www.baiud\",\"target\":\"\"}},\"2\":{\"type\":\"LINK\",\"mutability\":\"MUTABLE\",\"data\":{\"href\":\"www.wangye\",\"target\":\"\"}},\"3\":{\"type\":\"LINK\",\"mutability\":\"MUTABLE\",\"data\":{\"href\":\"www\",\"target\":\"\"}}}}";
                pythonService.jsonAnalysis(jsonMessage);
    }

    /**
     * @return
     */
    @ApiOperation(value = "数据导出", notes = "数据导出")
    @GetMapping("/excel/out/{id}/{name}/{courseName}")
    public void out(HttpServletResponse response, HttpServletRequest request,
                    @ApiParam(required = true, name = "id", value = "编号") @PathVariable("id") Integer id,
                    @ApiParam(required = true, name = "courseName", value = "课程名称") @PathVariable("courseName") String courseName,
                    @ApiParam(required = true, name = "name", value = "名称") @PathVariable("name") String name) throws IOException {

        List<PythonExcel> list = pythonService.selectAll(id, courseName);
        ExportParams params = new ExportParams();
        response.setHeader("content-Type", "application/octet-stream; charset=utf-8");
        response.setHeader("Content-Disposition", "attachment;filename="
                + URLEncoder.encode(name + new SimpleDateFormat("\"yyyyMMddhhmmss\"").format(new Date()), "UTF-8") + ".xls");
        response.setCharacterEncoding("UTF-8");
        Workbook workbook = ExcelExportUtil.exportExcel(params, PythonExcel.class, list);
        workbook.write(response.getOutputStream());
    }
}
